﻿namespace StackDeck
{
    using System.Windows;

    using Microsoft.VisualBasic.ApplicationServices;

    using StartupEventArgs = Microsoft.VisualBasic.ApplicationServices.StartupEventArgs;

    public class SingleInstanceApplicationWrapper : WindowsFormsApplicationBase
    {
        public SingleInstanceApplicationWrapper(IApplication application)
        {
            this.Application = application;

            // Enable single-instance mode.
            this.IsSingleInstance = true;
        }

        private IApplication Application { get; set; }

        private new SplashScreen SplashScreen { get; set; }

        // Create the WPF application class.        
        protected override bool OnStartup(StartupEventArgs e)
        {
            this.ShowSplashScreen();
            this.Application.Run(this.Application.RootWindow);            

            return false;
        }

        // Direct multiple instances.
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e)
        {
        }

        protected new void ShowSplashScreen()
        {
            if (string.IsNullOrWhiteSpace(this.Application.SplashScreenUri))
            {
                return;
            }

            this.SplashScreen = new SplashScreen(this.Application.SplashScreenUri);

            // Show the splash screen, with auto fade
            this.SplashScreen.Show(true);
        }
    }
}